package com.example.watchs.config;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.watchs.common.MonitorDTO;
import com.example.watchs.entity.MstAddrPath;
import com.example.watchs.methods.BaseMethod;
import com.example.watchs.service.MstAddrPathService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ieb
 */
@Configuration
@Data
public class FileMonitorConfig {
    private Map<Path, BaseMethod> conversionRules;
    private List<MonitorDTO> monitoredFolders;
    private MstAddrPathService mstAddrPathService;

    @Autowired
    public FileMonitorConfig(MstAddrPathService mstAddrPathService) {
        this.mstAddrPathService = mstAddrPathService;
    }

    /**
     * 初始化方法，将表中的所有监控值进行赋值，方便以后得管理
     */
    @PostConstruct
    private void init() {
        QueryWrapper<MstAddrPath> wrapper = Wrappers.query();
        List<MstAddrPath> paths = mstAddrPathService.list(wrapper);
        monitoredFolders = new ArrayList<>();
        conversionRules = new HashMap<>();
        for (MstAddrPath path : paths) {
            MonitorDTO monitorDTO = MonitorDTO.builder()
                    .maxDepth(path.getDeep())
                    .filePath(path.getFilePath())
                    .toFilePath(path.getOutputPath())
                    .types(path.getType() != null ? Arrays.stream(path.getType().split(",")).map(k -> {
                        try {
                            return Integer.valueOf(k);
                        } catch (Exception e) {
                            return null;
                        }
                    }).filter(Objects::nonNull).collect(Collectors.toList()) : null)
                    .isRun(false)
                    .build();
            /**
             * 设置默认监控值
             */
            if (CollectionUtils.isEmpty(monitorDTO.getTypes())) {
                List<Integer> types = new ArrayList<>();
                types.add(1);
                types.add(2);
                types.add(3);
                monitorDTO.setTypes(types);
            }
            try {
                /**
                 * 通过bean的方式寻找监控转换方法
                 */
                if (path.getMethod() == null) {
                    continue;
                }
                BaseMethod t = SpringUtil.getBean(path.getMethod());
                if (t != null) {
                    monitoredFolders.add(monitorDTO);
                    conversionRules.put(Paths.get(path.getFilePath()), t);
                } else {
                    System.out.println(path.getFilePath() + "未找到监控方法");
                }
            } catch (Exception e) {
                System.out.println("监控方法错误！" + e.getMessage());
            }
        }
    }
}